package cn.javabb.common.model;

import cn.javabb.common.enums.ResponseEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/11/04 16:33
 */

@Slf4j
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class ResponseModel {
    private boolean success;
    private Object data;
    private long total;
    private long pages;
    private String msg;
    private Integer code; //

    public boolean isSuccess() {
        return success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public static ResponseModel Success() {
        ResponseModel res = new ResponseModel();
        res.setSuccess(true);
        res.setCode(0);
        return res;
    }

    /**
     * 封装table grid
     * @param data
     * @return
     */
    public static ResponseModel Success(Object data) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(true);
        res.setData(data);
        if (data instanceof Page) {
            res.setTotal(((Page) data).getTotal());
            res.setPages(((Page) data).getPages());

        }else if(data instanceof PageInfo){
            res.setTotal(((PageInfo) data).getTotal());
            res.setPages(((PageInfo) data).getPages());
            res.setData(((PageInfo) data).getList());
        } else if (data instanceof Collection) {
            res.setTotal(((Collection) data).size());
        } else {
            res.setTotal(1);
        }
        return res;
    }

    public static ResponseModel Success(Object data, Integer code) {
        ResponseModel res = Success(data);
        res.setCode(code);
        return res;
    }

    public static ResponseModel Success(Object data, long total) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(true);
        res.setData(data);
        res.setTotal(total);
        return res;
    }

    public static ResponseModel Success(String msg) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(true);
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public static ResponseModel Failure() {
        ResponseModel res = new ResponseModel();
        res.setSuccess(false);
        res.setCode(1);
        return res;
    }

    public static ResponseModel Failure(String msg) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(false);
        res.setMsg(msg);
        return res;
    }

    public static ResponseModel Failure(Object data) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(false);
        res.setData(data);
        return res;
    }

    public static ResponseModel Failure(String msg, Integer code) {
        ResponseModel res = Failure(msg);
        res.setCode(code);
        return res;
    }

    public static ResponseModel NoLogin() {
        ResponseModel res = new ResponseModel();
        res.setSuccess(false);
        res.setCode(ResponseEnum.UNAUTHORIZED.getCode());
        res.setMsg(ResponseEnum.UNAUTHORIZED.getMessage());
        return res;
    }

    public static ResponseModel NoLogin(String msg) {
        ResponseModel res = new ResponseModel();
        res.setSuccess(false);
        res.setCode(401);
        res.setMsg(msg);
        return res;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }


    public static ResponseModel table(Object data) {
        ResponseModel res = Success(data);
        res.setCode(0);
        return res;
    }

}